//
//  CommentContentViewMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-12.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "CommentContentViewMaker.h"
#import "Config.h"
#import "Helper.h"
#import "ASImageView.h"

@implementation CommentContentViewMaker

// 画出视图原型
+ (void)makePrototype:(UIView *)view
{
    //头像
    ASImageView* avartImageView = [[ASImageView alloc] initWithFrame:CGRectMake(10, 5, _PICTURE_AVART_PREVIEW_WIDTH, _PICTURE_AVART_PREVIEW_HEIGHT)];
    [view addSubview:avartImageView];
    
    //名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, 100, 13)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = _SYS_FONT_002;
    [view addSubview:nameLabel];
    
    //日期
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width - 120, 5, 100, 15)];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.font = _SYS_FONT_001;
    dateLabel.textColor = _SYS_COLOR_002;
    [view addSubview:dateLabel];
    
    
    //设置评论内容,需要动态设定label的高
    UILabel* contentLable = [[UILabel alloc] initWithFrame:CGRectMake(45, 20, 250, 0)];
    contentLable.backgroundColor = [UIColor clearColor];
    contentLable.numberOfLines = 0;
    contentLable.lineBreakMode = UILineBreakModeWordWrap;
    contentLable.font = _SYS_FONT_002;
    [view addSubview:contentLable];
}

// 计算一个动态的高度
+ (float)heightWithDictionary:(NSDictionary *)data
{
    float height = 0.0f;
    
    //评论或者回复内容
    NSString *commentContent = [data objectForKey:@"content"];
    
    CGSize size = [commentContent sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(250, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
    height = size.height + 25;
    
    return height;
}

// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    //设置评论发布者的头像缩略图
    NSString *previewAvatar = [data objectForKey:@"creatorAvatarPreview"];
    ASImageView* avatarImageView = [view.subviews objectAtIndex:0];
    [avatarImageView setImageURL:[NSURL URLWithString:previewAvatar] andSourceImageURL:nil forStyle:PictureStyleAvartPreview];

    
    //设置评论人姓名
    NSInteger userId = [[data objectForKey:@"creatorID"] integerValue];   //评论发布者的Id
    NSString *realName = [data objectForKey:@"creatorRealName"];    //评论发布者的姓名
    //设置名字,判断是否是用户本人，如果是则显示“我”，不是则显示其他用户的realname
    UILabel *nameLabel = [[view subviews] objectAtIndex:1];
    nameLabel.text = (userId == [_ACCOUNT_ID integerValue]) ? @"我" : realName;

    
    //设置日期
    NSString *createTime = [data objectForKey:@"createTime"];
    UILabel *dateLabel = [[view subviews] objectAtIndex:2];
    dateLabel.text = [Helper formatDateString:createTime];
    
    
    //设置内容
    NSString *commentContent = [data objectForKey:@"content"];
    UILabel* contentLabel = [[view subviews] objectAtIndex:3];
    contentLabel.text = commentContent;
    
    //设置label高度
    CGSize size = [contentLabel.text sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(250, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
    contentLabel.frame = CGRectMake(contentLabel.frame.origin.x, contentLabel.frame.origin.y, contentLabel.frame.size.width, size.height);
}

@end
